#!/usr/bin/python
# coding:utf-8

"""
@author: 信长华
@contact: skywater@gmail.com
@software: PyCharm
@file: user_service.py
@time: 2021/7/30 15:27
"""

from ..common.api_response import ResponseEnum, BusinessException
from ..common.constant import USERNAME_DICT
from ..common.security import verify_password
from ..model.model import SysUser


def get_current_user(username: str) -> SysUser:
    """
    根据用户名获取当前用户
    :param username:
    :return:
    """

    user_dict = USERNAME_DICT.get(username)
    if not user_dict:
        raise BusinessException(ResponseEnum.TOKEN_ERROR)
    user = SysUser(**user_dict)
    return user


def authenticate_user(db, username: str, password: str):
    user_dict = db.get(username)
    if user_dict is None:
        return False
    user: SysUser = SysUser(**user_dict)
    if not verify_password(password, user.password):
        return False
    return user
